package vn.orderonline.core.commons.utils;

import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtils {

	private static ObjectMapper objectMapper = new ObjectMapper();
	
	public static String toJson(Object value) {
		try {
			return objectMapper.writeValueAsString(value);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static <T> T fromJson(String json, Class<T> T) {
		try {
			return objectMapper.readValue(json, T);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static <T> T fromMap(Map<String, Object> map, Class<T> T) {
		return objectMapper.convertValue(map, T);
	}

	@SuppressWarnings("unchecked")
	public static <T> Map<String, Object> toMap(T T) {
		return objectMapper.convertValue(T, Map.class);
	}
}
